/**
 * This file is part of Wireless TransFer.
 *
 *   Wireless TransFer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *	
 *   Wireless TransFer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Wireless TransFer.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	 Copyright (C) 2011
 *   @author ninjarobirob
 */
package com.wirelesstransfer.client;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

import com.wirelesstransfer.connection.ServerActions;

public class ClientConnectCheck implements Runnable {
	
	private long sleepTime;
	private volatile boolean isConnected;

	private String PREFIX = "http://";
	private String TAG = "ClientConnectCheck";
	
	private ClientConnection clientConnection;
	private HttpClient client;
	private ApacheClient apacheClient;
	
	public ClientConnectCheck(long sleepTime, HttpClient client) {
		this.sleepTime = sleepTime;
		this.client = new DefaultHttpClient();
		apacheClient = ApacheClient.getInstance();	
		clientConnection = apacheClient.getConnection();
		
	}
	
	
	public void run() {
		isConnected = true;
		try {
			System.err.println(clientConnection.getSessionId() + " thread ");


			while (isConnected) {

				HttpPost checkRequest = new HttpPost();
				checkRequest.setHeader(ServerActions.SESSIONID, clientConnection.getSessionId());
				checkRequest.setURI(new URI(PREFIX + clientConnection.getIP() + ":" + clientConnection.getPort() + "/" + ServerActions.CHECKACTIVITY));
				HttpResponse response = client.execute(checkRequest);
				System.err.println(clientConnection.getSessionId() + " still connected CLIENT ");

				Thread.sleep(sleepTime);
				if(!(response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK)) {
					Log.v(TAG, "is no longer connected");
					isConnected = false;
				}
				
			apacheClient.disconnect(clientConnection.getSessionId());
			Log.v(TAG, "end of while");
			
			}
		} catch (InterruptedException e) {
			Log.v(TAG, "Interrupdet");
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		client.getConnectionManager().shutdown();
	}
	
	public boolean disconnected() {
		isConnected = false;
		return isConnected;
	}
	
}


